package fallingGame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * <p>
 * Title: GameInput
 * </p>
 * <p>
 * Description: CS 343 Course Project GameInput is the controller in the Model
 * View Controller (MVC). It handles input from the user.
 * </p>
 * 
 * @author Nathanial Howard, Joe Costello, Dustin Schwingle, Jared Potter
 * @email nathanial.howard@my.uwrf.edu, anthony.costello@my.uwrf.edu,
 *        dustin.schwingle@my.uwrf.edu, jared.potter@my.uwrf.edu
 * @date December 01, 2011
 * @team Group 2
 */
public class GameInput implements KeyListener {

	// important key states needed for this program
	private boolean leftKey, rightKey, upKey;
	private boolean wasdMode;

	/**
	 * GameInput
	 * 
	 * default constructor
	 * 
	 * @ensure GameInput data members will be initialized to false
	 */
	GameInput() {
		wasdMode = false;
		this.leftKey = false;
		this.rightKey = false;
		this.upKey = false;
	}

	/**
	 * keyPressed
	 * 
	 * @param k
	 *            - this KeyEvent is caught by the interface listener The method
	 *            acts on it and sets key states of the class This is fired when
	 *            a button is pushed down on the keyboard.
	 * @ensure the key pressed will have its boolean data member set to true
	 */
	public void keyPressed(KeyEvent k) {

		// this is for input using arrow keys
		if (!wasdMode) {
			if (k.getKeyCode() == KeyEvent.VK_LEFT) {
				leftKey = true;
			}
			if (k.getKeyCode() == KeyEvent.VK_RIGHT) {
				rightKey = true;
			}
			if (k.getKeyCode() == KeyEvent.VK_UP) {
				upKey = true;
			}
		}

		// this is for input using WASD style
		if (wasdMode) {
			if (k.getKeyChar() == 'a') {
				leftKey = true;
			}
			if (k.getKeyChar() == 'd') {
				rightKey = true;
			}
			if (k.getKeyChar() == 'w') {
				upKey = true;
			}
		}
	}

	/**
	 * keyReleased
	 * 
	 * @param k
	 *            - this KeyEvent is caught by the interface listener The method
	 *            acts on it and sets key states of the class This is fired when
	 *            a button is released up on the keyboard.
	 * @ensure the key released will have its boolean data member set to false
	 */
	public void keyReleased(KeyEvent k) {
		// this is for input using arrow keys
		if (!wasdMode) {
			if (k.getKeyCode() == KeyEvent.VK_LEFT) {
				leftKey = false;
			}
			if (k.getKeyCode() == KeyEvent.VK_RIGHT) {
				rightKey = false;
			}
			if (k.getKeyCode() == KeyEvent.VK_UP) {
				upKey = false;
			}
		}

		// this is for input using WASD style
		if (wasdMode) {
			if (k.getKeyChar() == 'a') {
				leftKey = false;
			}
			if (k.getKeyChar() == 'd') {
				rightKey = false;
			}
			if (k.getKeyChar() == 'w') {
				upKey = false;
			}
		}
	}

	/**
	 * keyTyped
	 * 
	 * @override This method does nothing. It is required by the interface,
	 *           however.
	 */
	public void keyTyped(KeyEvent k) {
		/* This method is not useful to this program. */
		/* However it is required to be implemented */
	}

	/**
	 * setWasdMode
	 * 
	 * setter for wasdMode
	 * 
	 * @param wasdMode
	 * @require wasdMode parameter must be true or false
	 * @ensure this.wasdMode = wasdMode
	 */
	public void setWasdMode(boolean wasdMode) {
		this.wasdMode = wasdMode;
	}

	/**
	 * isWasdMode
	 * 
	 * getter for wasdMode
	 * 
	 * @return true - wasdMode is active, false - arrow keys are active
	 * @ensure wasdMode will be returned
	 */
	public boolean isWasdMode() {
		return wasdMode;
	}

	/**
	 * isLeft
	 * 
	 * getter for leftKey
	 * 
	 * @return true - left key is being pressed, false - left key is not being
	 *         pressed
	 * @ensure leftKey will be returned
	 */
	public boolean isLeft() {
		return leftKey;
	}

	/**
	 * isRight
	 * 
	 * getter for rightKey
	 * 
	 * @return true - right key is being pressed, false - right key is not being
	 *         pressed
	 * @ensure rightKey will be returned
	 */
	public boolean isRight() {
		return rightKey;
	}

	/**
	 * isUpKey
	 * 
	 * getter for upKey
	 * 
	 * @return true - up key is being pressed, false - up key is not being
	 *         pressed
	 * @ensure upKey will be returned
	 */
	public boolean isUpKey() {
		return upKey;
	}

}
